package com.bst.bsj.bops.web.biz.impl;

import java.util.List;
import java.util.Map;
import com.bst.bsj.common.dal.daointerface.MessageDAO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bst.bsj.bops.web.biz.MessageService;
import com.bst.bsj.common.dal.dataobject.Message;
import com.homewaimai.common.lang.Paginator;
import com.homewaimai.common.lang.Result;
import com.homewaimai.common.lang.ResultSupport;


@Service
public class MessageServiceImpl implements MessageService {

  private static final Log logger = LogFactory.getLog(MessageServiceImpl.class);

  @Autowired
  private MessageDAO messageDAO;

  @SuppressWarnings("unchecked")
  @Override
  public Result query(Map<String, Object> paramMap) {
    Result result = new ResultSupport(false);

    Integer length = (Integer) paramMap.get("length");
    Integer page = (Integer) paramMap.get("page");
    Long total = messageDAO.getCounts(paramMap);
    List<Message> messageList = messageDAO.selectAll(paramMap);
    // 创建一个分页器(leng.intValue：每页多少条)
    Paginator paginator = new Paginator(length.intValue());
    // 设置总项数
    paginator.setItems(total == null ? 0 : total.intValue());
    // 设置当前页
    paginator.setPage(page);
    result.getModels().put("paginator", paginator);
    result.getModels().put("messageList", messageList);

    result.setSuccess(true);
    return result;
  }

  @Override
  public Long insert(Message message) {
    Long id=null;
    try {
      id= messageDAO.insert(message);
    } catch (Exception e) {
      logger.error("MessageServiceImpl.insert(message) error!", e);
    }
    return id;
  }

  @Override
  public int delete(Long id) {
    int lid = 0;
    try {
      lid = messageDAO.delete(id);
    } catch (Exception e) {
      logger.error("MessageServiceImpl.delete(id) error!", e);
    }
    return lid;
  }

}
